package br.com.main.client.system;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DeckPanel;

public class SistemaPresenter {

	private Display display;
	private DeckPanel deck;

	interface Display {
		DeckPanel getDeck();

		Button[] getBotoes();
	}

	public SistemaPresenter(Display display, DeckPanel deck) {
		this.display = display;
		this.deck = deck;
		bind();
	}

	private void bind() {
		display.getBotoes()[0].addClickHandler(new ClickBotoes(0));
		display.getBotoes()[1].addClickHandler(new ClickBotoes(1));
		display.getBotoes()[2].addClickHandler(new ClickBotoes(2));
		display.getBotoes()[3].addClickHandler(new ClickBotoes(3));
		display.getBotoes()[4].addClickHandler(new ClickBotoes(4));

		if (display.getBotoes().length == 8) {
			display.getBotoes()[5].addClickHandler(new ClickBotoes(5));
			display.getBotoes()[6].addClickHandler(new ClickBotoes(6));
			display.getBotoes()[7].addClickHandler(new ClickHandler() {

				@Override
				public void onClick(ClickEvent event) {
					deck.remove(2);
					deck.showWidget(0);
					display.getBotoes()[0].click();
				}
			});
		} else {
			display.getBotoes()[5].addClickHandler(new ClickHandler() {

				@Override
				public void onClick(ClickEvent arg0) {
					deck.remove(2);
					deck.showWidget(0);
					display.getBotoes()[0].click();
				}
			});
		}

	}

	private class ClickBotoes implements ClickHandler {

		private int i;

		public ClickBotoes(int i) {
			this.i = i;
		}

		@Override
		public void onClick(ClickEvent event) {
			display.getDeck().showWidget(i);
			display.getBotoes()[i].setStyleName("botaoClicado", true);

			for (int j = 0; j < display.getBotoes().length; j++) {
				if (i != j) {
					display.getBotoes()[j].setStyleName("botaoClicado", false);
				}
			}

		}

	}

}
